package org.shangrila.app.wechat.utils;

import org.shangrila.app.wechat.bean.DepartmentList;
import org.shangrila.app.wechat.bean.WechatUser;
import org.shangrila.app.wechat.bean.WxError;
import org.shangrila.app.wechat.config.RestTemplateConfiguration;
import org.shangrila.app.wechat.constant.WechatApiUrl;
import org.shangrila.app.wechat.menu.Menu;
import org.shangrila.app.wechat.notification.BaseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class WeChatHttpUtil {
	private static Logger logger = LoggerFactory.getLogger(WeChatHttpUtil.class);
	
	/**
	 * 创建用户
	 * @必填 userId      成员UserID。对应管理端的帐号，企业内必须唯一。不区分大小写，长度为1~64个字节
	 * @必填 userName    成员名称。长度为1~64个字符
	 * @必填 department  成员所属部门id列表,不超过20个。 例：[1, 2]
	 * @param wechatUser
	 * @return
	 */
	public static WxError createUser(WechatUser wechatUser) {
		String wxCreateUserUrl = WechatApiUrl.CREATE_USER+AccessWxUtil.mailAccessToken;
		String userJson = JSONObject.toJSONString(wechatUser);
		HttpEntity<String> entity = new HttpEntity<String>(userJson);
		WxError wxError = RestTemplateConfiguration.getRestTemplate().postForObject(wxCreateUserUrl, entity,
				WxError.class);
		logger.error("wxerrot 为：" + wxError.getErrcode() + "--" + wxError.getErrmsg());
		return wxError;
	}

	/**
	 * 创建菜单
	 */
	public static WxError createMenu(Menu menu) {
		String wxCreateMenuUrl = WechatApiUrl.CREATE_MENU + AccessWxUtil.accessToken;
		String menuJson = new Gson().toJson(menu);
		HttpEntity<String> entity = new HttpEntity<String>(menuJson);
		WxError wxError = RestTemplateConfiguration.getRestTemplate().postForObject(wxCreateMenuUrl, entity,
				WxError.class);
		logger.error("wxerrot 为：" + wxError.getErrcode() + "--" + wxError.getErrmsg());
		return wxError;
	}
	
	/**
	 * 获取部门列表
	 * @return
	 */
	public static DepartmentList getDepartmentList() {
		String getDepartMentUrl = WechatApiUrl.GET_DEPARTMENT_LIST + AccessWxUtil.accessToken;
		DepartmentList list = RestTemplateConfiguration.getRestTemplate().getForObject(getDepartMentUrl, DepartmentList.class);
		return list;
	}
	
	/*
	 * 获取菜单列表
	 */
	public static JsonObject getMenu() {
		String getMenuUrl = WechatApiUrl.GET_MENU.replace("ACCESS_TOKEN", AccessWxUtil.accessToken).replace("AGENTID", AccessWxUtil.agentid) ;
		ResponseEntity<String> responseEntity = RestTemplateConfiguration.getRestTemplate().getForEntity(getMenuUrl, String.class);
		String json = responseEntity.getBody();
		JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
		return jsonObject;
	}
	
	/**
	 * 网页授权，根据code获取用户信息
	 * @param code
	 * @return
	 */
	public static JsonObject getUserInfoByCode(String code) {
		String url = WechatApiUrl.GET_USER_INFO_BY_CODE.replace("ACCESS_TOKEN", AccessWxUtil.accessToken)
				.replace("CODE", code);
		ResponseEntity<String> responseEntity = RestTemplateConfiguration.getRestTemplate().getForEntity(url, String.class);
		String json = responseEntity.getBody();
		JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
		return jsonObject;
	}
	
	public static WxError sendMessage(BaseMessage msg) {
		String url = WechatApiUrl.SEND_MESSAGE.replace("ACCESS_TOKEN", AccessWxUtil.accessToken);
		String msgJson = JSONObject.toJSONString(msg);
		logger.error(msgJson);
		HttpEntity<String> entity = new HttpEntity<String>(msgJson);
		WxError wxError = RestTemplateConfiguration.getRestTemplate().postForObject(url, entity,
				WxError.class);
		return wxError;
	}
}
